package com.zx.znav.interceptor;

import com.zx.znav.controller.vo.UserVO;
import com.zx.znav.utils.UserHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 前台用户登录拦截器
 * @since 2023-08-08 14:18
 */
public class UserLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserVO user = UserHolder.getUser();
        if(user == null){   //拦截
            response.setStatus(401);
            return false;
        }
        //放行
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserHolder.removeUser();
    }
}
